package me.raih.service.impl;

import me.raih.mapper.UserMapper;
import me.raih.pojo.po.User;
import me.raih.pojo.vo.PageBean;
import me.raih.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author Raih
 * @Time 2020/11/27  17:10
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    PageBean<User> pageBean;
    @Override
    public List<User> findAllUsers() {

        return userMapper.findAllUsers();
    }

    @Override
    public PageBean<User> findUserByCurrAndCurrentPage(String username, String name, String email, String address, long phone, int currentPage, int pageSize) {

        if("".equals(username) || username == null){
            username = null;
        }
        else {
            username = "%"+username+"%";
        }
        if("".equals(name) || name == null){
            name = null;
        }
        else {
            name = "%"+name+"%";
        }
        if("".equals(email) || email == null){
            email = null;
        }
        else {
            email = "%"+email+"%";
        }
        if("".equals(address) || address == null){
            address = null;
        }
        else {
            address = "%"+address+"%";
        }

        int count = userMapper.findCountByCondition(username, name, email, phone, address);
        //设置查询的最大值数量
        pageBean.setTotalCount(count);
        //设置最大页
        int maxNum;
        if(count % pageSize == 0){
            maxNum = count/pageSize;
        }else {
            maxNum = (count/pageSize) + 1;
        }
        pageBean.setTotalPage(maxNum);
        if(currentPage > maxNum ){
            currentPage = maxNum;
        }
        if(currentPage < 1){
            currentPage = 1;
        }
        //设置当前页
        pageBean.setCurrentPage(currentPage);

        List<User> userList = userMapper.findUserByCurrentPageAndPageSize((currentPage-1)*pageSize, pageSize, username, name, email, phone, address);
        //设置分页的信息
        pageBean.setList(userList);
        //设置每页多少数据
        pageBean.setPageSize(pageSize);

        return pageBean;
    }

    @Override
    public User findUserByUsername(String username) {

        User user = userMapper.findUserByUsername(username);

        return user;
    }

    @Override
    public boolean canUpdateUserByUser(User user) {

        int i = userMapper.updateUserByUser(user);
        if(i == 1){
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public int canAddUser(User user) {

        User userName = userMapper.findUserByUserName(user.getUsername());

        if(userName == null){
            //说明不存在
            int i = userMapper.insertUser(user);
            if(i == 1){
                return 1;   //注册成功
            }else {
                return 0;  //注册失败
            }
        }else {
            //如果存在
            return -1;  //不能注册
        }

    }

    @Override
    public User findUserByPhone(long phone) {

        return userMapper.findUserByPhone(phone);

    }

    @Override
    public User findUserByUser(User user) {

        return userMapper.findUserByUsernameAndPassword(user.getUsername(), user.getPassword());
    }

    @Override
    public boolean registerUser(User user) {

        User userByUsername = userMapper.findUserByUsername(user.getUsername());
        //如果该用户名不存在
        if(userByUsername == null){
            user.setPoints(0.0);
            user.setRegisterTime(new Date());
            //注册用户
            userMapper.insertUser(user);
            return true;
        }
        return false;
    }

    @Override
    public boolean addUserPoint(String username,double points) {

        if(points > 0 && username != null){
            int i = userMapper.updatePointByUsername(username,points);
            return i == 1;
        }

        return false;
    }

    @Override
    public int findUserNumber() {
        int i = userMapper.findUserNumber();
        return Math.max(i,0);
    }

    @Override
    public int findTodayUserNumber() {
        int i = userMapper.findTodayUserNumber();
        return Math.max(i,0);
    }

    @Override
    public int findYesterdayUserNumber() {
        int i = userMapper.findYesterdayUserNumber();
        return Math.max(i,0);
    }

    @Override
    public int findCurrMonthUserNumber() {
        int i = userMapper.findCurrMonthUserNumber();
        return Math.max(i,0);
    }

    @Override
    public int findCurrWeekUserNumber() {
        int i = userMapper.findCurrWeekUserNumber();
        return Math.max(i,0);
    }

    @Override
    public boolean changeUserStatus(String username, String checkCode) {
        User user = userMapper.findUserByUsernameAndCheckCode(username,checkCode);
        if(user != null){
            int i = userMapper.changeStatus(username);
            return i == 1;
        }
        return false;
    }

    @Override
    public double findPointsByUsername(String username) {

        double points = userMapper.findPointsByUsername(username);
        if(points <= 0){
            points = 0;
        }
        return points;
    }

    @Override
    public boolean deleteUserByUsername(String username) {
        int i = userMapper.deleteUserByUsername(username);
        return i == 1;
    }
}
